package com.tt.mymu.controller;

import com.tt.mymu.entity.User;
import com.tt.mymu.entity.po.UserPO;
import com.tt.mymu.service.UserService;
import com.tt.mymu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登陆
     * @param userName
     * @param password
     * @param request
     * @param response
     * @return
     */
    @PostMapping("loginUser")
    public Result loginUser(String userName, String password, HttpServletRequest request, HttpServletResponse response) {
        return userService.loginUser(userName, password, request, response);
    }

    /**
     * 用户注册
     * @param user
     * @param request
     * @param response
     * @return
     */
    @PostMapping("registUser")
    public Result registUser(User user, HttpServletRequest request, HttpServletResponse response) {
        return userService.registUser(user, request, response);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("deleteUser")
    public Result deleteUser(int id) {
        return userService.deleteUser(id);
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PostMapping("updateUser")
    public Result updateUser(User user) {
        return userService.updateUser(user);
    }

    @PostMapping("getUserById")
    public Result getUserById(int id) {
        return userService.getUserById(id);
    }

    @PostMapping("getUserList")
    public Result getUserList(UserPO userPO,
                              @RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "size", defaultValue = "10") int size) {
        return userService.getUserList(userPO, page, size);
    }

    @PostMapping("getUserByToken")
    public Result getUserByToken(String token) {
        if (token == null) {
            Result.build(1,"数据为空");
        }
        return userService.getUserByToken(token);
    }

}
